//
//  LDOpenInvoiceHistory.swift
//  DingDongZP
//
//  Created by 三秒文化 on 2016/10/14.
//  Copyright © 2016年 51ledian.com. All rights reserved.
//

import UIKit

class DDOpenInvoiceHistory: LDBaseTableViewVC{
    
    
    var model:DDInvoiceHistoryModel?
    var listModel:DDInvoiceHistoryListModel?
    
    private let cellID = "DDOpenInvoiceHistoryCellID"
    //是否有隐藏标签
    private var isShowArr = [NSDictionary]()
    
    override func viewDidLoad() {
        
        
        tableView.separatorStyle = .None
    }
    
    override func viewDidAppear(animated: Bool) {
        
        requestNet()
    }
    
    //请求历史纪录
    func requestNet() {
        
        LDUserInfoNet.sharedInstance.getInvoiceBillHistoryList(nil, succ: { [weak self] (dictArr) in
           
            self?.model = DDInvoiceHistoryModel.yy_modelWithDictionary(dictArr)// ??
            
            var i = 0
            for item in (self?.model!.list ?? [NSDictionary]()) {
                
                let str = item["isdelete"] as? String ?? "0"
                
                if str  == "1" {
                    
                    self?.model?.list?.removeAtIndex(i)
                }
                i += 1
            }
            
            self?.tableView.reloadData()
        }) { (err) in
            
            print(err)
        }
    }
    
    
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        
        let vc = DDInvoiceHistoryDetailVC()
        vc.title = "开票历史纪录详情"
        let dict = model?.list![indexPath.row] ?? NSDictionary()

        vc.model = DDInvoiceHistoryListModel.yy_modelWithDictionary(dict as [NSObject : AnyObject])
        navigationController?.pushViewController(vc, animated: true)
    }
}

extension DDOpenInvoiceHistory {
    
    // MARK: - Table view data source

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return model?.list?.count ?? 0
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
        
        
        var cell = tableView.dequeueReusableCellWithIdentifier(cellID) as? DDOpenInvoiceHistoryListCell
        
        if cell == nil {
            cell = DDOpenInvoiceHistoryListCell.init(style: UITableViewCellStyle.Default, reuseIdentifier: "cellinvocie")
        }
        
        cell?.model = DDInvoiceHistoryListModel.yy_modelWithDictionary((model!.list![indexPath.row] ?? NSDictionary()) as [NSObject : AnyObject])
        return cell!
    }
    
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
        return 60
    }
    
}
